﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
    static GameManager instance;
    SceneFader fader;
    Door lockedDoor;
    List<OrbCollect> orbs;

    public static float gameTime;
    public RecordList victoryRecordList;

    int deathNum = 3;
    bool gameIsOver;
    PlayerData currentPlayer;

    private void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
            return;
        }
        instance = this;

        UIManager.UpdateDeathUI(deathNum);

        orbs = new List<OrbCollect>();
        currentPlayer = instance.gameObject.AddComponent<PlayerData>();

        DontDestroyOnLoad(this);//挂载脚本的组件一直存在
    }

    private void Start()
    {
        GameSaveManager.LoadRecord();
    }

    private void Update()
    {
        if (gameIsOver)
        {
            deathNum = 3;
            UIManager.UpdateDeathUI(deathNum);
            return;
        }
            
        gameTime += Time.deltaTime;

        UIManager.UpdateTimeUI(gameTime);
    }

    public static void PlayerCollectOrb(OrbCollect orb) //收集宝珠,减少登记宝珠的数量
    {
        if (!instance.orbs.Contains(orb))
            return;

        instance.orbs.Remove(orb);

        if (instance.orbs.Count == 0)
        {
            instance.lockedDoor.Open();
        }

        UIManager.UpdateOrbUI(instance.orbs.Count);
    }

    public static void RegisterOrb(OrbCollect orb) //登记场景中的所有宝珠
    {
        if (instance == null)
            return;
        if (SceneManager.GetActiveScene().name == "MainMenu")
            return;

        if (!instance.orbs.Contains(orb))
            instance.orbs.Add(orb);

        UIManager.UpdateOrbUI(instance.orbs.Count);
    }

    public static void RegisterDoor(Door obj) //登记门
    {
        instance.lockedDoor = obj;
    }

    public static void RegisterSceneFader(SceneFader obj) //登记场景幕布的渐变控制
    {
        instance.fader = obj;
    }

    public static void PlayerDied() //人物死亡
    {
        if (instance.deathNum <= 0)
        {
            instance.gameIsOver = true;
            instance.orbs.Clear();
            UIManager.DisplayGameOver();
            instance.Invoke("CloseGameOver", 1.3f);
            instance.Invoke("ResumeMainMenu", 1.5f);
            return;
        }

        instance.fader.FadeOut();
        instance.deathNum--;

        UIManager.UpdateDeathUI(instance.deathNum);
        instance.Invoke("RestartScene",1.5f);
    }

    public static void PlaerWon() //游戏胜利
    {
        instance.gameIsOver = true;
        AudioManager.PlayerWonAudio();
        UIManager.DisplayWin();
        instance.AddRecord();
        
        RegisterPlayerData(instance.currentPlayer);
        GameSaveManager.SaveRecord();
        instance.Invoke("CloseGameOver", 1.3f);
        instance.Invoke("ResumeMainMenu", 1.5f);
    }

    public static bool GameOver() //游戏结束监测
    {
        return instance.gameIsOver;
    }

    void RestartScene() //重启场景
    {
        instance.orbs.Clear();

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    void ResumeMainMenu() //返回主目录
    {
        instance.orbs.Clear();
        instance.gameIsOver = false;
        SceneManager.LoadScene("MainMenu");
    }

    void CloseGameOver() 
    {
        UIManager.CloseGameOver();
    }

    void AddRecord()//添加胜利记录
    {
        instance.currentPlayer.playerRecord = gameTime;
    }

    public static void RegisterPlayerData(PlayerData pd)//登记玩家数据
    {
        PlayerData newPD = instance.gameObject.AddComponent<PlayerData>();
        newPD.playerRecord = pd.playerRecord;
        newPD.playerName = pd.playerName;

        for (int i = 0; i < instance.victoryRecordList.recordList.Count; i++)
        {
            if (instance.victoryRecordList.recordList[i] == null)
            {
                instance.victoryRecordList.recordList[i] = newPD;
                break;
            }
        }
    }

    public static void SetPlayerName(string playerName)//记录当前玩家名
    {
        instance.currentPlayer.playerName = playerName;
    }

    
}
